﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace stockUploader.Core
{
    public class SlotMachine
    {
        bool[] slots;

        public SlotMachine(int maxSlots)
        {
            slots = new bool[maxSlots];
            for(var i = 0; i < maxSlots; i++)
                slots[i] = false;
        }

        public bool HasFreeSlot()
        {
            return slots.Any(s => !s);
        }

        public int GetFreeSlot()
        {
            while(!HasFreeSlot()) ;
            lock(this)
            {
                for(int i = 0; i < slots.Length; i++)
                    if(!slots[i])
                    {
                        slots[i] = true;
                        return i;
                    }
                return -1;
            }
        }

        public bool IsAllFree()
        {
            return !slots.Any(s => s);
        }


        public void FreeSlot(int idx)
        {
            slots[idx] = false;
        }

        public override string ToString()
        {
            string s = "";
            for(int i = 0; i < slots.Length; i++)
                s += slots[i] ? "1" : "0";
            return s;
        }
    }
}
